package com.example.phonestore.controller;

import com.example.phonestore.entity.User;
import com.example.phonestore.mapper.UserMapper;
import com.example.phonestore.service.UserService;
import com.example.phonestore.util.JsonResult;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController{



    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;

    @RequestMapping("login")
    public JsonResult login(String username, String password,HttpSession session){
        return userService.login(username,password,session);
    }

    @RequestMapping("register")
    public JsonResult register(String username, String password){
        return userService.register(username,password);

    }



    @RequestMapping("updatePassword")
    public JsonResult updatePassword(String oldPassword, String newPassword,HttpSession session){
        Integer uid=getuidFromSession(session);
        return userService.updatePassword(uid,oldPassword,newPassword);
    }


    @RequestMapping("showUser")
    public User showUser(HttpSession session){
        User data=userService.showUser(getuidFromSession(session));
        System.out.println(getuidFromSession(session));
        return data;
    }


    @RequestMapping("/updateUser")
    public JsonResult updateUser(User user,HttpSession session){
        Integer uid=getuidFromSession(session);
        user.setUid(uid);
        System.out.println("输出："+user);

       return userService.updateUser(user);
    }


}
